<?php
namespace App\Http\Controllers\Admin;
//店铺基本信息审核

use Illuminate\Http\Request; 
use App\BshopBaseInfo;
use App\Shop;

class BBaseInfoShenhe extends AdminController
{
    // 列表
    public function alist(Request $request)
    {
        $status = trim($request->input('status', ''));
        $category_id = trim($request->input('category_id', ''));//业态
        $shop_id = trim($request->input('shop_id', ''));
        $shop_name = trim($request->input('shop_name', ''));
        
        $fliter = BshopBaseInfo::where('shop_id', '>', 0);
         
        
        if($status){
            $fliter = $fliter->where('status', $status);
        }
        if($category_id){ 
            $fliter =  $fliter->whereIn('shop_id', Shop::where('category_id', $category_id)->pluck('member_id'));
        }
        if($shop_id){
            $fliter =  $fliter->where('shop_id', $shop_id);
        }
        if($shop_name){
            $fliter =  $fliter->whereIn('shop_id', Shop::where('company', 'like', "%$shop_name%")->pluck('member_id'));
        }
        $this->itemsWithPage($request, $fliter);  
    }
    
    public function shenhe(Request $request){
        $status = trim($request->input('status', ''));//2:审核通过,3:审核不通过
        $status_extra = trim($request->input('status_extra', ''));
        $id = $request->input('id');//2:审核通过,3:审核不通过
        $baseinfo = BshopBaseInfo::where('id', $id)->first();
        if(!$baseinfo){
            $this->err('id错误');
        }
        if($baseinfo->status != 1){
            $this->err('已审核不能再次审核');
        }
        $baseinfo->status = $status;
        $baseinfo->status_extra = $status_extra;
        if($baseinfo->save()){
            if($baseinfo->status == 2){
                $shopId =  $baseinfo->shop_id;
                $shopObj = Shop::where('member_id', $shopId)->first();
                $updateInfoItems = array('company', 'provinceid', 'areaid', 'townid', 'address', 'logo', 'description', 'description2', 'fanxian_bili');
                foreach ($updateInfoItems as $colnum){
                    $shopObj->$colnum = $baseinfo->$colnum; 
                }
                if($shopObj->save()){
                    $this->output(array(), '审核同步成功');
                }else{
                    $this->err('同步错误');
                }
            }
        }
        $this->output(array(), '审核完成');
    }
}